/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import db.DatabaseConnection2;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import util.GetHardDiskSeriNumber;

/**
 *
 * @author phat10130059
 */
public class CheckSerial extends javax.swing.JFrame {

    /**
     * Creates new form CheckSerial
     */
    private DatabaseConnection2 db2 = new DatabaseConnection2();

    public CheckSerial() {
        initComponents();

        setResizable(false);
        setSize(400, 100);
        setLocationRelativeTo(null);
        this.setIconImage(new javax.swing.ImageIcon(getClass().getResource("/img/icon.png")).getImage());

        if (checkExistHardDiskSeriNumber()) {
            Login login = new Login();
//            setVisible(true);
        } else {
            setVisible(true);
        }
        
//        setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtCode = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        btOk = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Enter Serial");
        getContentPane().setLayout(new java.awt.GridLayout(2, 1));
        getContentPane().add(txtCode);

        btOk.setText("Ok");
        btOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btOkActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(146, 146, 146)
                .addComponent(btOk, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(165, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btOk)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btOkActionPerformed

        boolean update = false;
        if (!txtCode.getText().trim().equals("")) {

            if (checkExistCodeVsHardDiskSeriNumber()) {
                
                
                db2.makeConnection();

                try {
                    String sqlUpdate = "UPDATE license "
                            + " set seri_hard_disk = ? "
                            + " where code = ? ";
                    System.out.println(sqlUpdate);
                    update = db2.executeUpdate(sqlUpdate, new String[]{GetHardDiskSeriNumber.seri(), txtCode.getText().trim()});


                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    db2.closeConnection();
                }

                if (update) {
                    JOptionPane.showMessageDialog(this, "Success", null, JOptionPane.INFORMATION_MESSAGE);
                    this.setVisible(false);
                    new Login();

                } else {
                    JOptionPane.showMessageDialog(this, "Error", null, JOptionPane.ERROR_MESSAGE);
                }
                
            }else{
                JOptionPane.showMessageDialog(this, "Serial fail!", "Error", JOptionPane.ERROR_MESSAGE);
            }

        }

    }//GEN-LAST:event_btOkActionPerformed

    private boolean checkExistHardDiskSeriNumber() {

        db2.makeConnection();

        try {
            String sql = "select * from license where seri_hard_disk = ? ";

            ResultSet rs = db2.executeQuery(sql, new String[]{GetHardDiskSeriNumber.seri()});

            if (rs.next()) {
                return true;
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            db2.closeConnection();
        }

        return false;
    }

    private boolean checkExistCodeVsHardDiskSeriNumber() {

        db2.makeConnection();

        try {
            String sql = "select * from license where code = ? ";

            ResultSet rs = db2.executeQuery(sql, new String[]{txtCode.getText().trim()});

            if (rs.next()) {
                
                String seriHardDisk = rs.getString("seri_hard_disk");
                if(seriHardDisk.equals("")){
                    return true;
                }
                
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            db2.closeConnection();
        }

        return false;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CheckSerial();
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btOk;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtCode;
    // End of variables declaration//GEN-END:variables
}
